package com.xmtlxx.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.CabinetBo;
import com.xmtlxx.wms.domain.vo.CabinetGroupVo;
import com.xmtlxx.wms.domain.vo.CabinetVo;
import com.xmtlxx.wms.service.ICabinetGroupService;
import com.xmtlxx.wms.service.ICabinetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 柜号Controller
 *
 * @author xmtlxx
 * @date 2022-12-15
 */
@Validated
@Api(value = "柜号控制器", tags = {"柜号管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/cabinet")
public class CabinetController extends BaseController {

    private final ICabinetService iCabinetService;

    private final ICabinetGroupService iCabinetGroupService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<CabinetVo> initBean(@Validated(QueryGroup.class) @RequestBody CabinetVo vo){
        if(StringUtils.isNotEmpty(vo.getGroupId())){
            CabinetGroupVo cabinetGroupVo = iCabinetGroupService.queryById(vo.getGroupId());
            vo.setAreaId(cabinetGroupVo.getAreaId());
            vo.setStatus("0");
            vo.setStoreId(cabinetGroupVo.getStoreId());
            vo.setUseDeptId(cabinetGroupVo.getUseDeptId());
            vo.setType(cabinetGroupVo.getType());
            vo.setLnums(0l);
            vo.setPnums(0l);
        }
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询柜号列表
     */
    @ApiOperation("查询柜号列表")
//    @SaCheckPermission("wms:cabinet:list")
    @GetMapping("/list")
    public TableDataInfo<CabinetVo> list(@Validated(QueryGroup.class) CabinetBo bo, PageQuery pageQuery) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        return iCabinetService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出柜号列表
     */
    @ApiOperation("导出柜号列表")
    @SaCheckPermission("wms:cabinet:export")
    @Log(title = "柜号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated CabinetBo bo, HttpServletResponse response) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        List<CabinetVo> list = iCabinetService.queryList(bo);
        ExcelUtil.exportExcel(list, "柜号", CabinetVo.class, response);
    }

    /**
     * 获取柜号详细信息
     */
    @ApiOperation("获取柜号详细信息")
    @SaCheckPermission("wms:cabinet:query")
    @GetMapping("/{id}")
    public R<CabinetVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iCabinetService.queryById(id));
    }

    /**
     * 新增柜号
     */
    @ApiOperation("新增柜号")
    @SaCheckPermission("wms:cabinet:add")
    @Log(title = "柜号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CabinetVo vo) {
        CabinetBo bo = new CabinetBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iCabinetService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改柜号
     */
    @ApiOperation("修改柜号")
    @SaCheckPermission("wms:cabinet:edit")
    @Log(title = "柜号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CabinetVo vo) {
        CabinetBo bo = new CabinetBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iCabinetService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除柜号
     */
    @ApiOperation("删除柜号")
    @SaCheckPermission("wms:cabinet:remove")
    @Log(title = "柜号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iCabinetService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @ApiOperation("批量新增架子")
    @SaCheckPermission("wms:cabinet:add")
    @Log(title = "新增架子", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addBatchCabinet")
    public R<Void> addBatchCabinet(@Validated(AddGroup.class) @RequestBody CabinetVo vo) {
        Boolean flag = iCabinetService.insertBatchByVo(vo);
        return toAjax( flag? 1 : 0);
    }
}
